﻿using System.Windows;
using System.Windows.Automation;
using System.Windows.Automation.Provider;
using System.Windows.Threading;

using DanielVaughan.Windows.Controls;

namespace DanielVaughan.Windows.Automation.Peers
{
    public class AppBarItemAutomationPeer : IInvokeProvider
    {
        readonly Dispatcher dispatcher = Deployment.Current.Dispatcher;
        public IAppBarItem Owner { get; private set; }

        public AppBarItemAutomationPeer(IAppBarItem appBarItem)
        {
            Owner = ArgumentValidator.AssertNotNull(appBarItem, "appBarItem");
        }

        public void Invoke()
        {
            if (!Owner.IsEnabled)
            {
                throw new ElementNotEnabledException();
            }

            dispatcher.InvokeIfRequired(() => Owner.PerformClick());
        }
    }
}